<template>
	<view class="pageAll">
		<view class="tagView" :class="{'tagViewHeight':currTag === 4}">
			<view class="subTagView" :class="{'subTagViewHeight':currTag === 4}">
				<tag @changeTag="changeTag" @changeSubTag="changeSubTag" :tagInfo="tagInfo" type="report" :currTag="currTag"
				 :subTagInfo="subTagInfo" :currSubTag="currSubTag" />
			</view>
		</view>
		<scroll-view scroll-y="true" class="reportList" :class="{'reportHeight':currTag === 4}" scroll-with-animation="true"
		 v-if="isAlready" @scrolltolower="scrollToLower ">
			<view v-for="(item, index) in list" :key="index" class="cardView" @click="showReport(currSubTag,item.id)">
				<foodReportCard @getList='getList' :info="item" :currTag="currTag" :currSubTag="currSubTag" @reportOperating="reportOperating" />
			</view>
			<view class="draftView" v-if="currTag === 1">
				
			</view>
		</scroll-view>
		<nonePage v-if="!(list && list.length > 0)" />
		<view class="bottomBtnView" v-if="currTag === 1">
			<button class="bottomBtn" @click="addFood" hover-class="subActive">
				添加食材
			</button>
		</view>
		<view class="boxShadow"></view>
	</view>
</template>

<script>
	import tag from '../../../components/foodReport/myTag.vue'
	import foodReportCard from '../../../components/foodReport/foodReportCard.vue'
	import nonePage from '../../../components/commonCom/nonePage.vue'
	let inter = null // 轮询
	export default {
		data() {
			return {
				isAlready: false,
				list: [],
				edit: [],
				reported: [],
				reviewing: [],
				reviewed: [],
				currTag: 0,
				currPage: 1, // 当前页面
				lastPage: 1, // 最后已页
				currSubTag: 1, // 已审核的字导航栏
				subTagInfo: [{
						title: '已通过',
						value: 1,
						tagNum: null,
					},
					{
						title: '未通过',
						value: 2,
						tagNum: null,
					},
					{
						title: '合作食材库',
						value: 3,
						tagNum: null,
					}
				],
				tagInfo: [{
						title: '编辑中',
						value: 1,
						tagNum: null,
					},
					{
						title: '已提报',
						value: 2,
						tagNum: null,
					},
					{
						title: '审核中',
						value: 3,
						tagNum: null,
					},
					{
						title: '已审核',
						value: 4,
						tagNum: null,
					}
				],
				isWorking: false, // 防止用户多次点击
				pageSize: 8,
			}
		},
		methods: {
			// 下拉刷新
			scrollToLower() {
				if (this.currPage < this.lastPage) {
					this.currPage++
					if (this.currTag === 4) {
						this.alreadyPass(this.currSubTag, true)
					} else {
						this.getReportList(this.currTag, true)
					}
				}
			},
			// 组件方法， 对提报进行操作
			reportOperating(value) {
				// 编辑中
				if (this.currTag === 1) {
					// 编辑提报
					if (value.value === 1) {
						this.$api.submissionMessageIn({
							id: value.id
						}).then(res => {
							if (res.status) {
								this.$store.commit('setReportDraftType2', res.data)
								uni.navigateTo({
									url: '../firstStep/firstStep'
								})
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1500,
								})
							}
						})
					} else {
						// 删除提报
						this.$api.submissionDelete({
							id: value.id
						}).then(res => {
							if (res.status) {
								uni.showToast({
									title: '删除提报成功',
								})
								this.getReportListFrist(this.currTag)
							} else {
								uni.showToast({
									title: '操作失败',
									icon: 'none',
								})
							}
						})

					}
				}
				// 已提报
				if (this.currTag === 2) {
					// 撤销提报
					if (value.value === 1) {
						this.$api.submissionWithdraw({
							id: value.id
						}).then(res => {
							if (res.status) {
								uni.showToast({
									title: '撤销提报成功',
								})
								this.currTag = 1
							} else {
								uni.showToast({
									title: '操作失败',
									icon: 'none',
								})
							}
						})
					}
				}
				// 已审核
				if (this.currTag === 4) {
					// 已通过 删除
					if (value.value === 1) {
						uni.navigateTo({
							url: '../signatureFile/signatureFile?id=' + value.id
						})
					} else {
						if (this.currSubTag === 2) {
							this.$api.submissionFileDelete({
								id: value.id
							}).then(res => {
								if (res.status) {
									uni.showToast({
										title: '删除提报成功',
									})
									this.getList(this.currSubTag)
								} else {
									uni.showToast({
										title: '操作失败',
										icon: 'none',
									})
								}
							})
						} else {
							this.$api.submissionConfirmationDelete({
								id: value.id
							}).then(res => {
								if (res.status) {
									uni.showToast({
										title: '删除提报成功',
									})
									this.getList(this.currSubTag)
								} else {
									uni.showToast({
										title: '操作失败',
										icon: 'none',
									})
								}
							})
						}

					}
				}
			},
			showReport(value, id) {
				if (this.currTag === 2 || this.currTag === 3) {
					uni.navigateTo({
						url: '../showReport/showReport?currTag=' + this.currTag + '&id=' + id
					})
				}
				if (this.currTag === 1) {
					this.$api.submissionMessageIn({
						id: id
					}).then(res => {
						if (res.status) {
							this.$store.commit('setReportDraftType2', res.data)
							uni.navigateTo({
								url: '../firstStep/firstStep'
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1500,
							})
						}
					})
				}
				if (this.currTag === 4) {
					if (parseInt(value) === 1 || parseInt(value) === 3) {
						uni.navigateTo({
							url: '../signatureFile/signatureFile?id=' + id
						})
					}
					if (parseInt(value) === 2) {
						uni.navigateTo({
							url: '../notPass/notPass?id=' + id
						})
					}
				}
			},
			addFood() {
				uni.navigateTo({
					url: '../firstStep/firstStep'
				})
			},
			changeTag(value) {
				this.currTag = value.value
				this.$store.commit('setCurrTag', value.value)
			},
			changeSubTag(value) {
				this.currSubTag = value.value
				this.$store.commit('setSubCurrTag', value.value)
			},
			getList(subTag){
				this.list = []
				if (subTag === 2) {
					this.$api.checkedFailList({
							page: 1,
							pagesize: this.pageSize,
						})
						.then(res => {
							if (res.status) {
								this.list = res.data.data
								this.lastPage = res.data.last_page
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1500,
								})
							}
						})
				} else {
					this.$api.checkedList({
							status: subTag === 1 ? 'be_confirmed' : 'confirmed',
							page: 1,
							pagesize: this.pageSize,
						})
						.then(res => {
							console.log(res)
							if (res.status) {
								this.list = res.data.data
								this.lastPage = res.data.last_page
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1500,
								})
							}
				
						})
				}
			},
			alreadyPass(subTag, isAdd) {
				if (subTag === 2) { 
					this.$api.checkedFailList({
							page: this.currPage,
							pagesize: this.pageSize,
						})
						.then(res => {
							if (res.status) {
								if (isAdd) {
									for (let index in res.data.data) {
										if (res.data.data.hasOwnProperty(index)) {
											this.list.push(res.data.data[index])
										}
									}
								}
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1500,
								})
							}
						})
				} else {
					this.$api.checkedList({
							status: subTag === 1 ? 'be_confirmed' : 'confirmed',
							page: this.currPage,
							pagesize: this.pageSize,
						})
						.then(res => {
							console.log(res)
							if (res.status) {
								if (isAdd) {
									for (let index in res.data.data) {
										if (res.data.data.hasOwnProperty(index)) {
											this.list.push(res.data.data[index])
										}
									}
									console.log(this.list)
								}
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1500,
								})
							}

						})
				}
			},
			getReportListFrist(newValue){
				let reportStatus = ''
				if (newValue === 1) {
					reportStatus = 'draft'
				} else if (newValue === 2) {
					reportStatus = 'submitted'
				} else if (newValue === 3) {
					reportStatus = 'audit'
				}
				this.list = null
				this.$api.selectList({
						status: reportStatus, // 状态（submitted:已提报，draft：草稿，audit：审核中）
						page: this.currPage,
						pagesize: this.pageSize,
					})
					.then(res => {
						if (res.status) {
							this.isAlready = true
							// if (isAdd) {
								// for (let index in res.data.data) {
									// if (res.data.data.hasOwnProperty(index)) {
										// this.list.push(res.data.data[index])
									// }
								// }
							// } else {
								this.list = res.data.data
							// }
							this.lastPage = res.data.last_page
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1500,
							})
						}
					})
			},
			getReportList(newValue, isAdd) {
				let reportStatus = ''
				if (newValue === 1) {
					reportStatus = 'draft'
				} else if (newValue === 2) {
					reportStatus = 'submitted'
				} else if (newValue === 3) {
					reportStatus = 'audit'
				}
				// this.list = null
				this.$api.selectList({
						status: reportStatus, // 状态（submitted:已提报，draft：草稿，audit：审核中）
						page: this.currPage,
						pagesize: this.pageSize,
					})
					.then(res => {
						if (res.status) {
							this.isAlready = true
							// if (isAdd) {
								for (let index in res.data.data) {
									if (res.data.data.hasOwnProperty(index)) {
										this.list.push(res.data.data[index])
									}
								}
							// } else {
								// this.list = res.data.data
							// }
							// this.lastPage = res.data.last_page
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1500,
							})
						}
					})
			},
			onshowChangeTag(value) {
				if (value === 4) {
					this.getList(this.currSubTag)
				} else {
					this.getReportListFrist(value)
				}
			},
			changeReportNum(value) {
				return false // 不需要消除数字，所以先不执行该函数
				if (value === 1) {
					this.tagInfo[0].tagNum = null
				}
				if (value === 2) {
					this.tagInfo[1].tagNum = null
				}
				if (value === 3) {
					this.tagInfo[2].tagNum = null
				}
				if (value === 4) {
					this.tagInfo[3].tagNum = null
				}
			},
			getReportSum (value) {
				this.$api.submissionSum({
					isNoNeedLoading: true
				})
					.then(res => {
						if (res.status) {
							this.tagInfo[0].tagNum = res.data.draft_count === 0 ? null : res.data.draft_count
							this.tagInfo[1].tagNum = res.data.submitted_count === 0 ? null : res.data.submitted_count
							this.tagInfo[2].tagNum = res.data.audit_count === 0 ? null : res.data.audit_count
							this.tagInfo[3].tagNum = res.data.submission_count === 0 ? null : res.data.submission_count
							this.subTagInfo[0].tagNum = res.data.submission_confirmation === 0 ? null : res.data.submission_confirmation
							this.subTagInfo[1].tagNum = res.data.submission_fail === 0 ? null : res.data.submission_fail
							this.changeReportNum(value)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1500,
							})
						}
					})
			},
			setInter() {
				clearInterval(inter)
				inter = setInterval(() => {
					this.getReportSum(this.currTag)
				}, 15000)
			},
		},
		components: {
			tag,
			foodReportCard,
			nonePage,
		},
		onReady() {
			this.currTag = 1 // 触发监听属性
		},
		onLoad() {
		},
		onUnload() {
			clearInterval(inter)
		},
		onHide() {
			clearInterval(inter)
		},
		onShow() {
			this.currPage = 1
			this.currTag = 0
			this.list = []
			this.currTag = parseInt(this.$store.getters.getCurrTag)
			this.currSubTag = parseInt(this.$store.getters.getSubCurrTag)
			this.onshowChangeTag(this.currTag)
			// 清除vuex里的草稿信息
			this.$store.commit('setReportDraftBeNone')
			this.getReportSum(this.currTag)
			this.setInter()
			
		},
		watch: {
			currTag(newValue, oldValue) {
				this.currPage = 1
				this.list = []
				if (newValue === 4) {
					this.getList(this.currSubTag)
				} else {
					this.getReportListFrist(newValue)
				}
				this.getReportSum(newValue)
			},
			currSubTag(newValue, oldValue) {
				this.currPage = 1
				this.getList(newValue)
			}
		},
		onShareAppMessage: (options) => {
			// 设置菜单中的转发按钮触发转发事件时的转发内容
			var shareObj = {
				title: "红标HONBIAO", // 默认是小程序的名称(可以写slogan等)
				path: '/pages/indexPage/authorization/authorization', // 默认是当前页面，必须是以‘/’开头的完整路径
				imageUrl: '/static/index/labelRouge.png', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
				success: (res) => {
					// 转发成功之后的回调
					if (res.errMsg == 'shareAppMessage:ok') {}
				},
				fail: () => {
					// 转发失败之后的回调
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						// 用户取消转发
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				}
			};
			return shareObj
		},
	}
</script>

<style scoped>
	.pageAll {
		box-sizing: border-box;
		background-color: #fdf5f3;
		width: 100%;
		height: 100%;
		display: inline-block;
		position: fixed;
	}

	.tagView {
		height: 12%;
		padding: 17upx 17upx 0 17upx;
		box-sizing: border-box;
		position: relative;
		z-index:2;
	}

	.tagViewHeight {
		height: 20% !important;
	}

	.subTagView {
		height: 75%;
	}

	.subTagViewHeight {
		height: 100% !important;
	}

	.reportList {
		height: 88%;
		overflow: auto;
		-webkit-overflow-scrolling: touch;
		box-sizing: border-box;
		position: relative;
		z-index: 1;
	}

	.boxShadow {
		box-shadow: 0upx 0upx 20upx 5upx rgba(76, 49, 40, 0.15);
		position: fixed;
		bottom: 0upx;
		width: 100%;
		z-index: 100;
		height: 1upx;
	}

	.reportHeight {
		height: 80%;
	}


	.cardView {
		min-height: 180upx;
		margin: 0 17upx 17upx 17upx;
	}

	.cardView:first-child {
		padding-top: 17upx;
	}

	.draftView {
		height:1px;
		width:100%;
		padding-bottom: 200upx;
	}

	.bottomBtnView {
		position: fixed;
		bottom: 66.66upx;
		width: 100%;
		z-index: 3;
	}

	.bottomBtn {
		margin: 0 auto;
		width: 368upx;
		height: 100upx;
		line-height: 100upx;
		letter-spacing: 20upx;
		text-indent: 20upx;
		color: #FFF;
		background-color: #f7533e;
	}

	.bottomBtn:after {
		border: none;
	}

	.subActive {
		background-color: #ff0000 !important;
	}
</style>
